package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.pojo.TbContent;
import com.taotao.service.ContentService;

@Controller
@RequestMapping("/content")
public class ContentController {
	@Autowired
	private ContentService contentService;

	/**
	 * 查询内容列表
	 * 
	 * @param categoryId
	 * @param page
	 * @param rows
	 * @return
	 * @author: JSJ
	 */
	@ResponseBody
	@RequestMapping("/query/list")
	public EasyUIDataGridResult getContentList(long categoryId, Integer page,
			Integer rows) {
		EasyUIDataGridResult result = contentService.getContentList(categoryId,
				page, rows);
		return result;
	}

	/**
	 * 保存新增内容
	 * 
	 * @param tbContent
	 * @return
	 * @author: JSJ
	 */
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_CONTENT_SYNC_URL}")
	private String REST_CONTENT_SYNC_URL;

	@ResponseBody
	@RequestMapping("/save")
	public TaotaoResult insertContent(TbContent tbContent) {
		TaotaoResult result = contentService.insertContent(tbContent);
		// 后台保存新增类目内容时,调用taotao-rest服务清空redis中的对应key缓存数据
		String url = REST_BASE_URL + REST_CONTENT_SYNC_URL
				+ tbContent.getCategoryId();
		HttpClientUtil.doGet(url);

		return result;
	}

	/**
	 * 保存修改的内容
	 * 
	 * @param tbContent
	 * @return
	 * @author: JSJ
	 */
	@ResponseBody
	@RequestMapping("/edit")
	public TaotaoResult updateContent(TbContent tbContent) {
		TaotaoResult result = contentService.updateContent(tbContent);
		// 后台保存修改的类目内容时,调用taotao-rest服务清空redis中的对应key缓存数据
		String url = REST_BASE_URL + REST_CONTENT_SYNC_URL
				+ tbContent.getCategoryId();
		HttpClientUtil.doGet(url);
		return result;
	}

	/**
	 * 批量删除内容
	 * 
	 * @param ids
	 * @return
	 * @author: JSJ
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public TaotaoResult deleteContent(long[] ids) {
		// 后台删除类目内容前,调用taotao-rest服务清空redis中的对应key缓存数据
		TbContent tbContent = contentService.getTbContentById(ids[0]);
		long cid = tbContent.getCategoryId();
		String url = REST_BASE_URL + REST_CONTENT_SYNC_URL
				+ tbContent.getCategoryId();
		HttpClientUtil.doGet(url);

		// 后台删除内容
		TaotaoResult result = contentService.deleteContent(ids);
		return result;
	}
}
